<?php
/**
 * 公司后台账号表模型
 */

namespace app\admin\model;


use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use custom\Redis;
use custom\Token;
use think\Db;

class CompanyAccount extends BaseModel {

    /**
     * 添加
     * @param $parmas
     */
    public static function post($params) {
        return self::insert($params);
    }

    /**
     * 删除公司下面所有账户
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function del($id) {
        return self::where('company_id', $id)->update(['deleted' => 1]);
    }

    /**
     * 重置密码
     * @param $params
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function resetPwd($params) {
        $data = db('company')->where('id',$params['id'])->find();
        if (empty($data)) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $rand = rand(100000, 999999);
        $password = md5($rand);
        $accountId = db('company_account')->where('company_id', $params['id'])->value('id');
        self::where('company_id', $params['id'])->update(['password' => $password]);
        $redisModel = Redis::getInstance();
        $redisModel->hdel(RedisKey::BADMIN_TOKEN_HASH . '_' . Token::BADMIN_TOKEN, $accountId);
        return $rand;
    }
}
